# -*- coding: UTF-8 -*-
#!/usr/bin/python2

import xlrd
import sys
import requests
from ocr import downloadFromJielong,ocr
import os.path

reload(sys)
sys.setdefaultencoding('utf8')

data_wb = xlrd.open_workbook('~/Desktop/ag/check-2022-05-03.xlsx')
result_wb  = xlrd.open_workbook('~/Desktop/ag/2022-05-03.xls')
data_sheet = data_wb.sheet_by_index(0)

result = {}
pepopleCountNeed = 0 
roomNeeded= 0
for r in xrange(3,31):
	row_data = data_sheet.row_values(r)
	for l in (0,3,6,9,12,15):
		room = row_data[l]
		if isinstance(room,unicode):
			room = room.replace('*','')
		elif isinstance(room,float):
			room = int(room)
		if l <= 6:
			room = '5-%s' % (room)
		else:
			room = '6-%s' % (room)
		pepopleCountNeed = pepopleCountNeed +  row_data[l+1]
		if isinstance(row_data[l+1],float) and row_data[l+1]>0:
			result[room]={'needed':int(row_data[l+1]),'actual':0,'room':room}

# print "应测试户数: %d " % (len(result))
roomNeeded = len(result)

#handle the result from wechat
result_sheet= result_wb.sheet_by_index(0)
# print result_sheet.hyperlink_map

pepopleCountActual = 0
for r in xrange(1,result_sheet.nrows):
	room = result_sheet.row_values(r)[0]
	if room[0]=='5':
		room=room[1:]
		# room = room.replace('5','')
		room = room.replace('-','')
		room='5-'+room
	if room[0]=='6':
		room=room[1:]
		# room = room.replace('6','')
		room = room.replace('-','')
		room='6-'+room

	if result.has_key(room):
		try:
			# print result_sheet.row_values(r)
			actual = result_sheet.row_values(r)[2]
			if isinstance(actual,unicode) and len(actual)>0:
				actual.encode('utf-8')
				if len(actual)>=1:
					actual= actual[0:1]
			elif isinstance(actual,float):
				actual = int(actual)
			else:
				actual = 0
			pepopleCountActual = pepopleCountActual + int(actual)
			if actual >0 and result[room]['needed']==int(actual):
				#calc count
				del result[room]
			else:
				result[room]['actual']=result_sheet.row_values(r)[2]
				result[room]['reason']=''
				result[room]['remark']=result_sheet.row_values(r)[5]
				url = result_sheet.row_values(r)[4]
				if result[room]['actual'] >0 and  len(url)>0 :
					url = result_sheet.hyperlink_map.get((r,4)).url_or_path
					#download the image the tmp
					filename = './image/%s.png'  % (result[room]['room'])
					if not os.path.isfile(filename):
						downloadFromJielong(url,filename)
						result[room]['reason']=ocr(filename)						
		except Exception as e:
			print e
			print result_sheet.row_values(r)
			result[room]['actual']=result_sheet.row_values(r)[2]

print '=============55-56抗原统计============'
print "应测试人数: %d.   实际测试人数: %d " % (pepopleCountNeed,pepopleCountActual)
print "应测试户数: %d.   数据异常户数 : %d  抗原异常户数:0" % (roomNeeded,len(result))
for r in result.keys():
	if len(result[r]['actual'])>0:
		print('房号:%s 应做:%s 实际:  %s 原因:%s,-> %s '% (result[r]['room'],result[r]['needed'],result[r]['actual'],result[r]['reason'],result[r]['remark']))
print '====================================='
unTest =0
for r in result.keys():
	if len(result[r]['actual'])==0:
		unTest = unTest +1
		print('房号:%s 应做:%s 实际:  %r 原因:%s '% (result[r]['room'],result[r]['needed'],result[r]['actual'],result[r]['reason']))
print '未测试： %d' % (unTest)
print '====================================='
